package fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.administrator.weipiao.R;

import java.util.ArrayList;
import java.util.List;

import adapter.MovieAdapter;
import bean.Movies;
import loadresource.Constants;
import loadresource.LoadJsonTask;

/**
 * A simple {@link Fragment} subclass.
 */
public class MovieFragment extends Fragment implements LoadJsonTask.OnGetJsonResultListener {

    private ListView mLv;
    private MovieAdapter mAdapter;
    private List<ImageView> images;

    public MovieFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_movie, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        final ViewPager mVp = (ViewPager) view.findViewById(R.id.vp);
        final int[] imagesId = getImage();
        images = new ArrayList<>();
        for (int i=0;i<imagesId.length;i++){
            ImageView img = new ImageView(view.getContext());
            img.setBackgroundResource(imagesId[i]);
            images.add(img);
        }
        assert mVp != null;
        mVp.setAdapter(new MyAdapter());
        //解决出使情况下，不能向左滑动的小bug
        mVp.setCurrentItem(10000);


        mLv = (ListView) view.findViewById(R.id.lv_movie);

//        mLv.addHeaderView(mVp);
        //执行下载Json的异步任务
        new LoadJsonTask(this).execute(Constants.PAHT1);
//        new LoadJsonTask(this).execute(Constants.JSON_URL);

        mAdapter = new MovieAdapter(view.getContext());
        mLv.setAdapter(mAdapter);
    }
    public int[] getImage(){
        return new int[]{R.drawable.movie1,R.drawable.movie2,R.drawable.movie3,R.drawable.movie4,R.drawable.movie5,R.drawable.movie6,R.drawable.movie7};
    }
    class MyAdapter extends PagerAdapter {
        //决定了ViewPager内部子控件的数量
        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }
        //用来初始化，产生一个条目，类似于getView()方法
        //container：指的就是ViewPager本身
        //position：每个字控件的位置
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(images.get(position%images.size()));
            return images.get(position%images.size());
        }
        //判断这个view和object是否相等
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }
        //移除某个条目
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(images.get(position%images.size()));
        }
    }
    //回调接口
    @Override
    public void onGetJsonResult(List<Movies> result) {
        mAdapter.setData(result);
    }
}
